import type { IQuery, TableConfig } from '@/api/config/types';
import type { UserDept, UserProfileData } from '@/api/modules/system/user/types';
import { hasPerm } from '@/utils/common/perm';
import { DeleteOutlined, EditOutlined, LockOutlined } from '@ant-design/icons-vue';
import type { ItemType } from 'ant-design-vue';
import { userDelete, userEdit } from './curd';
import { passwordModal } from './form';

//查询参数接口
interface UserQuery {
  userName: string;
  nickName: string;
  email: string;
  phonenumber: string;
  sex: string;
  status: string;
  deptId: number | undefined;
}

let queryObj: IQuery<UserQuery> = {
  pageNum: 1,
  pageSize: 10,
  total: 0,
  userName: '',
  nickName: '',
  email: '',
  phonenumber: '',
  sex: '',
  status: '',
  deptId: undefined,
};
const resetUserQuery = () => {
  userTable.value.query = {
    ...queryObj,
  };
};

export const userTable = ref<TableConfig<UserProfileData>>({
  //勾选的唯一标识,数据库主键
  rowKey: 'userId',
  data: [],
  loading: false,
  //必须唯一，存储窗口位置，表头等
  moduleName: 'SystemUser',
  viewMode: 'table',
  keys: [],
  query: queryObj,
  contextmenu: [
    {
      title: '修改密码',
      label: '修改密码',
      key: 'update-pwd',
      onClick() {
        passwordModal.value = true;
      },
      icon: h(LockOutlined),
      disabled: hasPerm('ax:user:edit'),
    },
    {
      title: '编辑',
      label: '编辑',
      key: 'edit',
      onClick() {
        userEdit();
      },
      icon: h(EditOutlined),
      disabled: hasPerm('ax:user:edit'),
    },
    {
      title: '删除',
      label: '删除',
      key: 'delete',
      icon: h(DeleteOutlined),
      onClick() {
        userDelete();
      },
      disabled: hasPerm('ax:user:remove'),
    },
  ],
  reset: resetUserQuery,
});

export const deptTreeData = ref<UserDept[]>([]);
